﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Persistance;
using MesErreurs;
using Metier;
using Utilitaires;



namespace Gestion_commerciale
{
    public partial class Fcommercial : Form
    {
        public Fcommercial()
        {
            InitializeComponent();
        }

        private void Fcommercial_Load(object sender, EventArgs e)
        {
            connexionServeur(sender,e);
        }

        private void connexionServeur(object sender, EventArgs e)
        {
            try
            {
                bool rep;
                // on connecte l'application à la base de données
                rep = OuvertureApplication.getOuverture();
                // si la connexion a réussie : on active les menus
                if (rep)
                {
                    connexionToolStripMenuItem.Enabled = false;
                    déconnexionToolStripMenuItem.Enabled = true;
                    interrogerToolStripMenuItem.Enabled = true;
                    gérerToolStripMenuItem.Enabled = true;

                    lbl_etat.Text = "Etat : en ligne";
                    lbl_etat.ForeColor = Color.DarkGreen;
                }
                // sinon on désactive les menus
                else
                {
                    connexionToolStripMenuItem.Enabled = true;
                    déconnexionToolStripMenuItem.Enabled = false;
                    interrogerToolStripMenuItem.Enabled = false;
                    gérerToolStripMenuItem.Enabled = false;

                    lbl_etat.Text = "Etat : hors ligne - erreur de connexion";
                    lbl_etat.ForeColor = Color.Red;
                }
            }
            catch (MonException excep)
            {
                MessageBox.Show(excep.MessageSysteme(), "Erreur de connexion");
                connexionToolStripMenuItem.Enabled = true;
                déconnexionToolStripMenuItem.Enabled = false;
                interrogerToolStripMenuItem.Enabled = false;
                gérerToolStripMenuItem.Enabled = false;
                lbl_etat.Text = excep.MessageSysteme();
                lbl_etat.ForeColor = Color.Red;
            }
        }

        private void deconnexionServeur(object sender, EventArgs e)
        {
            try
            {
                bool rep;
                // on connecte l'application à la base de données
                rep = FermetureApplication.getFermeture();
                // si la connexion a réussie : on active les menus
                if (!rep)
                {
                    connexionToolStripMenuItem.Enabled = false;
                    déconnexionToolStripMenuItem.Enabled = true;
                    interrogerToolStripMenuItem.Enabled = true;
                    gérerToolStripMenuItem.Enabled = true;

                    lbl_etat.Text = "Etat : en ligne";
                    lbl_etat.ForeColor = Color.DarkGreen;
                }
                // sinon on désactive les menus
                else
                {
                    connexionToolStripMenuItem.Enabled = true;
                    déconnexionToolStripMenuItem.Enabled = false;
                    interrogerToolStripMenuItem.Enabled = false;
                    gérerToolStripMenuItem.Enabled = false;

                    lbl_etat.Text = "Etat : hors ligne";
                    lbl_etat.ForeColor = Color.Red;
                }
            }
            catch (MonException excep)
            {
                MessageBox.Show(excep.MessageSysteme(), "Erreur de deconnexion");
                connexionToolStripMenuItem.Enabled = false;
                déconnexionToolStripMenuItem.Enabled = true;
                interrogerToolStripMenuItem.Enabled = true;
                gérerToolStripMenuItem.Enabled = true;
                lbl_etat.Text = excep.MessageSysteme();
                lbl_etat.ForeColor = Color.Red;
            }
        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Menu_fiche_client_Click(object sender, EventArgs e)
        {
            FClient fc= new FClient();
            fc.ShowDialog();
        }

        private void listeCommandeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FListeCdes flcde= new FListeCdes();
            flcde.ShowDialog();
        }

        private void ajouterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FInser_Cde finser = new FInser_Cde();
            finser.ShowDialog();
        }

        private void ficheVendeurToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Fvendeur fv = new Fvendeur();
            fv.ShowDialog();
        }

        private void commandeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FCommande fC = new FCommande();
            fC.ShowDialog();
        }

        private void articleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FArticle fA = new FArticle();
            fA.ShowDialog();
        }
    }
}
